<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Promise封装AJAX函数e</title>
</head>
<body>

<script>
  function sendAJAX(url){
    return new Promise((resolve,reject)=>{
      const xhr=new XMLHttpRequest()
      xhr.open('GET',url)
      xhr.send()
      xhr.onreadystatechange=function () {
        if (xhr.readyState === 4){
          if (xhr.status>=200&&xhr.status<300){
            resolve(xhr.response)
          }else{
            reject(xhr.status)
          }
        }
      }
    })
  }

  //调用封装之后的sendAJAX函数，返回的是一个已经写好逻辑的promise对象
sendAJAX('https://api.apiopen.top/getJoke').then((value)=>{
  console.log(value)},(reason)=>{
  console.log(reason)
        }

)
</script>
</body>
</html>